﻿/************************************************************************

 This file is part of "10 Second Tim".

 "10 Second Tim" is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 "10 Second Tim" is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with "10 Second Tim".  If not, see <http://www.gnu.org/licenses/>.

************************************************************************/

// All code (c)2013 Scott Bevin. all rights reserved

using LDE;
using LDE.Input;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Media;

#if !SILVERLIGHT

namespace LudumDare27.GameScreens
{
	internal class OptionsMenuScreen : GameScreen
	{
		private SpriteFont font;

		private WhiteFadeScreen whiteFadeOut = null;
		private GameScreen gamescreenToTrigger = null;
		private Texture2D logoTexture;

		private Menu menu;

		public OptionsMenuScreen( )
			: base( true )
		{
		}

		public override void OnAdded( )
		{
			font = ContentManager.Load<SpriteFont>( "Fonts/DebugFont" );

			menu = new Menu( ContentManager.Load<SpriteFont>( "Fonts/DebugFont" ), ContentManager.Load<SpriteFont>( "Fonts/DebugFont" ), ContentManager );
			menu.OnBack += MainMenu;

			logoTexture = ContentManager.Load<Texture2D>( "Img/GameLogo" );

#if !SILVERLIGHT
			menu.AddEntry( SoundEffect.MasterVolume > 0.0f ? "Sfx: On" : "Sfx: Off", this.SfxToggle );
			menu.AddEntry( MediaPlayer.IsMuted ? "Music: Off" : "Music: On", this.MusicToggle );
			menu.AddEntry( Game.graphics.PreferMultiSampling ? "MSAA: On" : "MSAA: Off", this.MSAAToggle );
			menu.AddEntry( Game.graphics.IsFullScreen ? "Fullscreen: On" : "Fullscreen: Off", this.FSToggle );
			menu.AddNonClickEntry( "", null );
			menu.AddNonClickEntry( "", null );
#endif

			//menu.AddEntry(MediaPlayer.IsMuted ? "Music: Off" : "Music: On", this.MusicToggle);
			menu.AddEntry( "Return To Main Menu", this.MainMenu );

			base.OnAdded( );
		}

		public override void Update( float dt )
		{
			menu.Update( dt, Player.Get.GetInput( this ), Game.GraphicsDevice );

			/*if (whiteFadeOut == null)
			{
				if (input.MouseButtonStates[(int)InputManager.MouseButtons.LeftButton].Released ||
					input.MouseButtonStates[(int)InputManager.MouseButtons.RightButton].Released)
				{
					whiteFadeOut = new WhiteFadeScreen();
					gamescreenToTrigger = new MainMenuScreen();

					GameScreenManager.AddGameScreen(whiteFadeOut, null, LudumDare26.GameScreenManager.AddGameScreenData.PositioningOptions.OnTop);
				}
			}*/

			if ( whiteFadeOut != null && whiteFadeOut.State == WhiteFadeScreen.States.FadedIn )
			{
				whiteFadeOut.FadeOut( );
				GameScreenManager.AddGameScreen( gamescreenToTrigger, whiteFadeOut, GameScreenManager.AddGameScreenData.PositioningOptions.BelowRef );
				this.KillScreen = true;
			}

			base.Update( dt );
		}

		public override void Render( float dt, GraphicsDevice gd, SpriteBatch sprites )
		{
			Vector2 basePosition = new Vector2( ( (float)gd.PresentationParameters.BackBufferWidth ) * 0.5f,
				( (float)gd.PresentationParameters.BackBufferHeight ) * 0.25f );

			sprites.Begin( SpriteSortMode.Deferred, BlendState.NonPremultiplied );

			sprites.Draw( logoTexture, basePosition, null, Color.White, 0.0f,
				new Vector2( ( (float)logoTexture.Width ) * 0.5f, ( (float)logoTexture.Height ) * 0.5f ), Vector2.One, SpriteEffects.None, 0.0f );

			//basePosition.Y = ((float)gd.PresentationParameters.BackBufferHeight) * 0.9f;

			//sprites.Draw(helpTexture, basePosition, null, Color.White, 0.0f,
			//	new Vector2(((float)helpTexture.Width) * 0.5f, ((float)helpTexture.Height) * 0.5f), Vector2.One, SpriteEffects.None, 0.0f);

			sprites.End( );

			menu.Render( gd, sprites );

			base.Render( dt, gd, sprites );
		}

		private void SfxToggle( Menu.MenuEntry entry )
		{
			if ( SoundEffect.MasterVolume > 0.0f )
			{
				Game.Settings.Settings.SfxOn = false;
				SoundEffect.MasterVolume = 0.0f;
				entry.Text = "Sfx: Off";
			}
			else
			{
				Game.Settings.Settings.SfxOn = true;

				SoundEffect.MasterVolume = 0.25f;
				entry.Text = "Sfx: On";
			}

			Game.Settings.Save( );
		}

		private void MusicToggle( Menu.MenuEntry entry )
		{
			if ( !MediaPlayer.IsMuted )
			{
				MediaPlayer.IsMuted = true;
				entry.Text = "Music: Off";
			}
			else
			{
				MediaPlayer.IsMuted = false;
				entry.Text = "Music: On";
			}

			Game.Settings.Save( );
		}

		private void MSAAToggle( Menu.MenuEntry entry )
		{
			if ( !Game.graphics.PreferMultiSampling )
			{
				Game.Settings.Settings.MSAAOn = true;

				Game.graphics.PreferMultiSampling = true;
				Game.graphics.ApplyChanges( );
				entry.Text = "MSAA: On";
			}
			else
			{
				Game.Settings.Settings.MSAAOn = false;

				Game.graphics.PreferMultiSampling = false;
				Game.graphics.ApplyChanges( );

				entry.Text = "MSAA: Off";
			}

			Game.Settings.Save( );
		}

		private void FSToggle( Menu.MenuEntry entry )
		{
			if ( !Game.graphics.IsFullScreen )
			{
				Game.Settings.Settings.FullScreen = true;

				Game.graphics.PreferredBackBufferWidth = Game.GraphicsDevice.DisplayMode.Width;
				Game.graphics.PreferredBackBufferHeight = Game.GraphicsDevice.DisplayMode.Height;
				Game.graphics.IsFullScreen = true;
				Game.graphics.ApplyChanges( );
				entry.Text = "Fullscreen: On";
			}
			else
			{
				Game.Settings.Settings.FullScreen = false;

				Game.graphics.PreferredBackBufferWidth = Defs.ScreenWidth;
				Game.graphics.PreferredBackBufferHeight = Defs.ScreenHeight;
				Game.graphics.IsFullScreen = false;
				Game.graphics.ApplyChanges( );

				entry.Text = "Fullscreen: Off";
			}

			Game.Settings.Save( );
		}

		private void MainMenu( Menu.MenuEntry entry )
		{
			whiteFadeOut = new WhiteFadeScreen( );
			gamescreenToTrigger = new MainMenuScreen( );

			GameScreenManager.AddGameScreen( whiteFadeOut, null, GameScreenManager.AddGameScreenData.PositioningOptions.OnTop );
		}
	}
}

#endif